import { getUserInfo, login } from '@/api/user'
import { getToken, setToken,setUser,getUser } from "@/utils/auth";
const actions = {
  login({ commit },userInfo){
    console.log(userInfo)
    const {idNumber , password} = userInfo
    return new Promise((resolve, reject) => {
      login({ idNumber: idNumber.trim(), password: password }).then(response => {
        console.log(response,'login')
        const { id,type } = response.data
          console.log(id,'uid')
          const token = id
          commit('LOGIN',{id,type})
          setToken(token)
          setUser({id,type})
          resolve()
      }).catch(err=>{
        console.log(err,'error')
        reject(err)
      })
    })
  },
  getUserInfoReq(){

  }
}

const mutations = {
  LOGIN(state,user){
    state.user = user
    state.id = user.id
  }
}

const state = {
  user:getUser(),
  id:getToken()
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}